package com.zhengxing.jienci.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhengxing.jienci.common.pojo.CustomizePage;
import com.zhengxing.jienci.common.support.SimpleResponse;
import com.zhengxing.jienci.common.util.DateUtil;
import com.zhengxing.jienci.common.util.IdGenerateUtil;
import com.zhengxing.jienci.user.domain.PointRule;
import com.zhengxing.jienci.user.service.IPointRuleService;
import com.zhengxing.jienci.web.context.SecurityContext;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.*;

/**
 * <p>
 * 积分规则 前端控制器
 * </p>
 *
 * @author yyh
 * @since 2020-12-01
 */
@Api(tags = "积分规则管理")
@RestController
@RequestMapping("/pointRule")
public class PointRuleController {

	@Autowired
	private IPointRuleService iPointRuleService;

	/**
	 * 通过id查询
	 */
	@ApiOperation("规则详情")
	@GetMapping("/getById/{id}")
	public PointRule getById(@PathVariable(value = "id") Integer id) {
		return iPointRuleService.getById(id);
	}

	/**
	 * 新增
	 */
	@ApiOperation("添加规则")
	@PostMapping("/save")
	public SimpleResponse save(@RequestBody PointRule pointRule) {
		pointRule.setId(IdGenerateUtil.getId());
		iPointRuleService.save(pointRule);
		return SimpleResponse.success();
	}

	/**
	 * 通过id删除
	 */
	@ApiOperation("删除规则")
	@DeleteMapping("/deleteById/{id}")
	public SimpleResponse delete(@PathVariable(value = "id") String ids) {
		String[] idsStrs = ids.split(",");
		for (String id : idsStrs) {
			iPointRuleService.removeById(id);
		}
		return SimpleResponse.success();
	}

	/**
	 * 修改
	 */
	@ApiOperation("修改规则")
	@PutMapping("/update")
	public SimpleResponse updateById(@RequestBody PointRule pointRule) {
		iPointRuleService.updateById(pointRule);
		return SimpleResponse.success();
	}

	/**
	 * 查询列表
	 */
	@ApiOperation("列表查询")
	@GetMapping("/list")
	public List<PointRule> list(PointRule pointRule) {
		final LambdaQueryWrapper<PointRule> lambda = new QueryWrapper<PointRule>().lambda();
		this.buildCondition(lambda, pointRule);
		return iPointRuleService.list(lambda);
	}

	/**
	 * 分页查询
	 */
	@ApiOperation("分页查询")
	@GetMapping("/page")
	public IPage<PointRule> page(CustomizePage page, PointRule pointRule) {
		LambdaQueryWrapper<PointRule> lambda = new QueryWrapper<PointRule>().lambda();
		this.buildCondition(lambda, pointRule);
		Page<PointRule> pages = new Page<PointRule>(page.getPageNow(), page.getPageSize());
		IPage<PointRule> resultPage = iPointRuleService.page(pages, lambda);
		return resultPage;
	}
	
	/**
	 * 获取人积分获取方式
	 */
	@ApiOperation("获取人积分获取方式")
	@GetMapping("/myselfAndToday")
	public List<PointRule> pointRuleListForMyself() {
		String userId = SecurityContext.getUserId();
		String nowDate = DateUtil.getNowDate();
		return iPointRuleService.pointRuleListForMyself(userId, nowDate);
	}

	/**
	 * 构造查询条件
	 * 
	 * @param lambda
	 * @param param
	 */
	@SuppressWarnings("unchecked")
	private void buildCondition(LambdaQueryWrapper<PointRule> lambda, PointRule param) {
		if (!StringUtils.isEmpty(param.getId())) {
			lambda.eq(PointRule::getId, param.getId());
		}
		if (!StringUtils.isEmpty(param.getOperation())) {
			lambda.eq(PointRule::getOperation, param.getOperation());
		}
		if (!StringUtils.isEmpty(param.getRule())) {
			lambda.eq(PointRule::getRule, param.getRule());
		}
		if (!StringUtils.isEmpty(param.getNum())) {
			lambda.gt(PointRule::getNum, param.getNum());
		}
		lambda.orderBy(true, false, PointRule::getId);
	}

}
